#include "stdafx.h"
#include "xmlParser.h"
#include "Parameter.h"


unsigned int SEED = 1230;
int GOSSIP_ROUND = 0;
int BUDGET_NUM = 16;
int BUDGET_SIZE = 0;
int TEST_INDEX = 1;
int DURATION = 100000;
double USER_ARRIVAL_RATE = 4;
int MEAN_ONLINE_DURATION  = 600000;
int * ROUTER_DISTANCE_TABLE = NULL;;
unsigned int NODE_COUNT=0;
int SAMPLING_NUMBER = 10;
//int CLUSTER_SIZE = 100;
int PUBLISH_SEARCHDEPTH = 0;
int PUBLISH_REDUNDANCE = 0;
int PARALLELISM = 0;
int JOIN_SEARCHDEPTH = 0;
int CLUSTER_RADIUS = 64;
int QUERY_SEARCHDEPTH = 15;

int SEARCH_STEPS  = 1;
int REPORT_ROUND = 0;
INT SNAPSHOT_ROUND = 0;
int DATA_INSERT_ROUND = 0;
int DATA_INSERT_NUMBER = 0;
int SEARCH_NODE_ROUND = 0;
int SEARCH_DATA_ROUND = 0;
int MULTIP_RANGE_DATA_SEARCH_TIME = 2400000;
int REPUBLISH_DATA_ROUND = 0;
int DATA_SEARCH_START_TIME = 0 ;
char * DATA_PATH_NAME;
char * SEED_PATH_NAME;
char * RANGE_SEED_PATH_NAME;
int MIN_SEARCH_RANGE;
int MAX_SEARCH_RANGE;
int SEARCH_RANGE_STEP;
int GetInt(XMLNode &xMainNode,const char *pName)
{
	XMLNode xGossipRound = xMainNode.getChildNode(pName);
	return (int)strtol(xGossipRound.getText(), NULL, 10);
};
double GetDouble(XMLNode &xMainNode,const char *pName)
{
	XMLNode xGossipRound = xMainNode.getChildNode(pName);
	return strtod(xGossipRound.getText(), NULL);
};

void GetText(XMLNode &xMainNode,const char *pName, string &out)
{
	XMLNode xGossipRound = xMainNode.getChildNode(pName);
	out = xGossipRound.getText();
};
bool GetParameter(const char *pStr )
{
	try
	{
		XMLNode xMainNode ;
		if(pStr == NULL)
		{
			xMainNode = XMLNode::openFileHelper("parameter\\Parameter.xml", "SimilaritySearchConfigure");
		}
		else
		{
			xMainNode = XMLNode::openFileHelper(pStr, "SimilaritySearchConfigure");
		}
		XMLNode xGossipRound = xMainNode.getChildNode("GOSSIP_ROUND");
		GOSSIP_ROUND = (int)strtol(xGossipRound.getText(), NULL, 10);

		XMLNode xRouterInfoLength = xMainNode.getChildNode("BUDGET_NUM");
		BUDGET_NUM = (int)strtol(xRouterInfoLength.getText(),NULL,10);
		ROUTER_DISTANCE_TABLE = new int[BUDGET_NUM];
		XMLNode xRouterDisTab = xMainNode.getChildNode("ROUTER_DISTANCE_TABLE");
		string strTable(xRouterDisTab.getText());
		strTable+=',';
		for(int i = 0;i<BUDGET_NUM;i++)
		{
			string strSub = strTable.substr(0,strTable.find_first_of(","));
			strTable = strTable.substr(strTable.find_first_of(",")+1);
			ROUTER_DISTANCE_TABLE[i] = (int)strtol(strSub.c_str(),NULL,10);
		}


		XMLNode xNodeCount = xMainNode.getChildNode("NODE_COUNT");
		NODE_COUNT = (unsigned int)strtol(xNodeCount.getText(), NULL, 10);
		
		XMLNode xReportRound = xMainNode.getChildNode("REPORT_ROUND");
		REPORT_ROUND = (unsigned int)strtol(xReportRound.getText(), NULL, 10);
		
		TEST_INDEX = GetInt(xMainNode,"TEST_INDEX");
		CLUSTER_RADIUS = GetInt(xMainNode,"CLUSTER_RADIUS");
		//CLUSTER_SIZE = GetInt(xMainNode,"CLUSTER_SIZE");
		BUDGET_SIZE = GetInt(xMainNode,"BUDGET_SIZE");
		PUBLISH_SEARCHDEPTH = GetInt(xMainNode,"PUBLISH_SEARCHDEPTH");
		PUBLISH_REDUNDANCE = GetInt(xMainNode,"PUBLISH_REDUNDANCE");
		PARALLELISM = GetInt(xMainNode,"PARALLELISM");
		BUDGET_SIZE = GetInt(xMainNode,"BUDGET_SIZE");
		JOIN_SEARCHDEPTH = GetInt(xMainNode,"JOIN_SEARCHDEPTH");
		SAMPLING_NUMBER = GetInt(xMainNode,"SAMPLING_NUMBER");
		DATA_INSERT_ROUND = GetInt(xMainNode,"DATA_INSERT_ROUND");
		DATA_INSERT_NUMBER = GetInt(xMainNode,"DATA_INSERT_NUMBER");
		SEARCH_NODE_ROUND = GetInt(xMainNode,"SEARCH_NODE_ROUND");
		SEARCH_DATA_ROUND = GetInt(xMainNode,"SEARCH_DATA_ROUND");
		USER_ARRIVAL_RATE = GetDouble(xMainNode,"USER_ARRIVAL_RATE");
		MEAN_ONLINE_DURATION  = GetInt(xMainNode,"MEAN_ONLINE_DURATION");
		REPUBLISH_DATA_ROUND = GetInt(xMainNode,"REPUBLISH_DATA_ROUND");
		DATA_SEARCH_START_TIME = GetInt(xMainNode,"DATA_SEARCH_START_TIME");
		QUERY_SEARCHDEPTH = GetInt(xMainNode,"QUERY_SEARCHDEPTH");
		SNAPSHOT_ROUND = GetInt(xMainNode,"SNAPSHOT_ROUND");
		SEED = GetInt(xMainNode,"SEED");
		SEARCH_STEPS = GetInt(xMainNode,"SEARCH_STEPS");
		DURATION = GetInt(xMainNode,"DURATION");
		MULTIP_RANGE_DATA_SEARCH_TIME = GetInt(xMainNode,"MULTIP_RANGE_DATA_SEARCH_TIME");
		string tmp; 
		GetText(xMainNode,"DATA_PATH_NAME",tmp );
		DATA_PATH_NAME = new char[tmp.size()+3];
		strcpy_s(DATA_PATH_NAME,tmp.size()+3,tmp.c_str());

		GetText(xMainNode,"SEED_PATH_NAME",tmp );
		SEED_PATH_NAME = new char[tmp.size()+3];
		strcpy_s(SEED_PATH_NAME,tmp.size()+3,tmp.c_str());

		GetText(xMainNode,"RANGE_SEED_PATH_NAME",tmp );
		RANGE_SEED_PATH_NAME = new char[tmp.size()+3];
		strcpy_s(RANGE_SEED_PATH_NAME,tmp.size()+3,tmp.c_str());
		MIN_SEARCH_RANGE = GetInt(xMainNode,"MIN_SEARCH_RANGE");
		MAX_SEARCH_RANGE = GetInt(xMainNode,"MAX_SEARCH_RANGE");
		SEARCH_RANGE_STEP = GetInt(xMainNode,"SEARCH_RANGE_STEP");
	}
	catch(...)
	{
		printf("Error Get Parameter");
		exit(0);
	}
	return true;
}